﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Mahasiswa.WP.ViewModels;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Mahasiswa.WP
{
    public partial class MahasiswaAddPage : PhoneApplicationPage
    {
        public MahasiswaAddPage()
        {
            InitializeComponent();
        }

        public async Task InsertMahasiswa()
        {
            try
            {
                MahasiswaModel mahasiswa = new MahasiswaModel();
                mahasiswa.Nama = TextBox_Nama.Text;
                mahasiswa.Alamat = TextBox_Alamat.Text;
                mahasiswa.Jurusan = TextBox_Jurusan.Text;
                mahasiswa.Umur = Convert.ToInt32(TextBox_Umur.Text);

                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.PostAsJsonAsync("http://169.254.80.80/akademik/api/Mahasiswa", mahasiswa);
                response.EnsureSuccessStatusCode();

                if (response.IsSuccessStatusCode)
                {
                    MessageBox.Show("Data berhasil disimpan.");
                }
                else
                {
                    MessageBox.Show("Data gagal disimpan.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ApplicationBarIconButton_Add_Click(object sender, EventArgs e)
        {
            InsertMahasiswa();
        }
    }
}